package com.bing.controller;

import com.bing.bean.exception.BingException;
import com.bing.bean.exception.TestException;
import com.bing.entity.TestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * Created by kaibb on 15-11-26.
 */

@Controller
@RequestMapping("/test")
public class HelloController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

    @RequestMapping("hello")
    public String index(@RequestHeader("User-Agent") String userAgent, Model model) {
        model.addAttribute("UserAgent", userAgent);
        return "hello";
    }


    @RequestMapping("log")
    @ResponseBody
    public String log() {
        LOGGER.error("hello log back");
        LOGGER.info("hello log back");
        return "hello logback";
    }

    @RequestMapping("charset/{value}")
    @ResponseBody
    public String charset(@PathVariable String value) {
        LOGGER.info("hello 乱码问题:{} ", value);
        return "hello 中文不应该乱码: " + value;
    }

    @RequestMapping("i18n")
    public String i18n(Locale locale) {

        LOGGER.info("测试国际化:{}", locale);
        return "i8n";
    }


    @RequestMapping("exception")
    public String exception() {
        LOGGER.info("测试异常");
        throw new TestException("测试异常");
    }

    @RequestMapping("noAuth")
    public String noAuth() {
        LOGGER.info("测试异常ResponseStatus");
        throw new BingException("测试异常ResponseStatus");
    }

    @ExceptionHandler(value = TestException.class)
    public ModelAndView handlerTestException(TestException testException) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error");
        mav.addObject("exception", testException.getMessage());
        return mav;
    }

    @RequestMapping("noSuch")
    public ModelAndView noSuchRequest(HttpServletRequest request) throws NoSuchRequestHandlingMethodException {
        throw new NoSuchRequestHandlingMethodException(request);
    }

    @RequestMapping("editor/{path}")
    public String editor(@PathVariable String path) {
        return "editor/" + path;
    }

    @RequestMapping("addTest")
    @ResponseBody
    public TestEntity addTest(@Validated @ModelAttribute TestEntity testEntity,BindingResult result) {
        LOGGER.info("addTest: {},result:{}", testEntity,result);
        return testEntity;
    }

}
